package com.spring.demo.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.spring.demo.pojo.Book;
import com.spring.demo.service.IBookService;
import com.spring.demo.user.pojo.User;
import com.spring.demo.user.service.IUserService;

/**
 *
 * @author nickel
 * @date 2019-01-25 16:19:52
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;

	@GetMapping("/save")
	public ModelAndView editUser(User user, ModelAndView mv) {
		mv.addObject(user);
		mv.setViewName("user/UserEdit");
		return mv;
	}

	@PostMapping("/save")
	public ModelAndView addBook(User user, ModelAndView mv) {
		userService.save(user);
		mv.addObject(user);
		mv.setViewName("redirect:/user/list");
		return mv;
	}

	@RequestMapping("/list")
	public ModelAndView listBook(ModelAndView mv) {
		mv.addObject("users", userService.list());
		mv.setViewName("user/UserList");
		return mv;
	}
}
